package bank.service.impl;

import bank.dao.AccountDao;
import bank.pojo.Account;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import bank.service.AccountService;
import org.springframework.transaction.annotation.Transactional;


@Service("accountService")
@Transactional
public class AccountServiceImpl implements AccountService {

    @Resource(name="accountDao")
    private AccountDao accountDao;

    @Override
    public void transfer(String fromActno, String toActno, double money) {


        //查询转出账户的余额是否充足
        Account FromAccount = accountDao.selectByActno(fromActno);

        if(FromAccount.getBalance()<money){
            throw new RuntimeException("余额不足");
        }
        Account ToAccount = accountDao.selectByActno(toActno);
        FromAccount.setBalance(FromAccount.getBalance()-money);
        ToAccount.setBalance(ToAccount.getBalance()+money);
        int update = accountDao.update(FromAccount);
        update+= accountDao.update(ToAccount);
        if(update !=2){
            throw new RuntimeException("转账失败,联系银行！");
        }
    }
}
